package com.dyh.quickdemozwei.hxfz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Slf4j
public class TpHxfz2 {

    /*影响了抛异常行为*/
    interface ExceptionChanger{}

    /*抛错型*/
    interface Thrower extends ExceptionChanger{}

    /*捕获型*/
    interface Catcher extends ExceptionChanger{}


    /* extends Catcher*/
    /*extends Thrower*/
    /*此基团 具备校验入参非空*/
    interface InputNotNull { }

    /*此基团 具备报错自动重试3次*/
    interface Retry3{ }

    /*化学反应表  如何变基*/
    Map<Class,Function<Function,Function>> reactionMap = new HashMap
            <>();
    {
        reactionMap.put(InputNotNull.class,f-> e->{
            if (e==null) throw new RuntimeException("param null");
            return f.apply(e);
        });

        reactionMap.put(Retry3.class,f-> e -> {
            int rc = 0;
            Object res = null;
            for (; ; ) {
                try {
                    res = f.apply(e);
                } catch (Exception ex) {
                    log.info("重试次数{}",rc);
                    if (rc++ >= 3)
                        break;
                    else
                        continue;
                }
                break;
            }
            return res;
        });

    }

//    <T,R> R  enchanceWork(T t, Function<T,R> base){
//       Class[] cls = base.getClass().getInterfaces();
//       /*序号0 是base*/
//        Thrower.class.isAssignableFrom(base.getClass().getInterfaces()[1]);
//    };


    @Test
    public void test(){
//        Function fc1 = (Function<String,Integer> &InputNotNull &Retry3) String::length;



        Function failFc = e->{
            if (1==1) throw new RuntimeException();
            return "dawd";
        };

        Function enhanceFc = reactionMap.get(Retry3.class).apply(failFc);

        enhanceFc.apply("dawd");

//        System.out.println(fc1.getClass().getInterfaces());
//        System.out.println(fc2.getClass().getInterfaces());


        System.out.println();
    }


//    public void printInterfaces(Object obj){
//        StringBuilder sb = new StringBuilder();
//        for (Class<?> anInterface : obj.getClass().getInterfaces()) {
//            sb.append(anInterface.getSimpleName()).append(",");
//        }
//        System.out.println(sb);
//    }


    @Test public void test4(){


        String str = "{\n" +
                "    \"webdavUser\":\"pim1@apacpro.com\",\n" +
                "    \"webdavPassword\":\"i$]S.(G&NTcdb\\\\\"2F\"\n" +
                "}";

        JSONObject jo2 = JSON.parseObject(str);

        System.out.println();
    }
}
